import { Component, OnInit } from '@angular/core';
import { UserService } from "../user.service";
import { Router,ActivatedRoute } from "@angular/router";
import * as moment from 'moment';
import { UploadService } from "../../../providers/upload.service";
@Component({
  selector: 'app-change',
  templateUrl: './change.component.html',
  styleUrls: ['./change.component.css']
})
export class ChangeComponent implements OnInit {
    name;
    isUpload:boolean = false;
   changeInfo = {
     userName:'',
     passWord:'',
     confirm:'',
     email:'',
     signature:'',
     icon:'',
     isChanged:'',
     newName:''
   }
   tempname
  alerts: any = [
  ]
  constructor(public userService:UserService, public router: Router,public actRoute:ActivatedRoute,public uploadService:UploadService) { }

  ngOnInit() {
    this.actRoute.params.subscribe(
      params => { 
        this.changeInfo.userName= window.localStorage.getItem("userName");
        this.changeInfo.icon= window.localStorage.getItem("icon");
        this.changeInfo.email= window.localStorage.getItem("email");
        this.changeInfo.signature= window.localStorage.getItem("signature");
       this.tempname = window.localStorage.getItem("icon")}
    );
  }
  openDialog() {
    this.success();
  }
  upload(e){
    this.uploadService.upload(e.files[0],(err, data)=>{
      this.changeInfo.icon = data.url + e.files[0].name;
       });
     }


  uploadd(e){
    this.isUpload = true;
    this.changeInfo.icon = "assets/temp/"+e.files[0].name;
    this.name = e.files[0].name;
  }
  success() {
    this.router.navigate(['user/profile']);
  }
  submit(){
    // if(this.changeInfo.icon === 'assets/img/default.jpg'){
    //   this.changeInfo.icon = 'default.jpg';
    //   this.changeInfo.isChanged = '0';
    // }else if(!this.isUpload){
    //   this.changeInfo.icon = this.tempname.substring(this.changeInfo.icon.lastIndexOf('/')+1,this.tempname.length);
    //   this.changeInfo.isChanged = '1';
    // }else{
    //   this.changeInfo.newName = moment().format('YYYYMMDDhhmmss') + this.changeInfo.icon.substring(this.changeInfo.icon.lastIndexOf('.'),this.changeInfo.icon.length);
    //   this.changeInfo.icon = this.name;
    //   this.changeInfo.isChanged = '2';
    // }
    this.changeInfo.icon = this.changeInfo.icon.replace(/&/g,'%26');
    if (this.changeInfo.confirm === this.changeInfo.passWord && this.changeInfo.passWord!='') {
      this.userService.change(this.changeInfo).subscribe(data => {
        if (data.json().status == true) {
          this.openDialog();
        } else if (data.json().status == false) {
          this.alerts.push({
            type: 'danger',
            msg: `<strong>Warning!</strong> 已存在用户名！`,
            timeout: 5000
          });
        } else {
          this.alerts.push({
            type: 'danger',
            msg: `<strong>Warning!</strong> 发生未知错误，请联系admin！`,
            timeout: 5000
          });
        }
      })
    } else {
      this.alerts.push({
        type: 'danger',
        msg: `<strong>Warning!</strong> 重复密码错误或密码为空！`,
        timeout: 5000
      });
    }
  }
}
